import { EnumUserType } from '../../utils/constant.js'
import util from '../../utils/util'
const app = getApp(),url = app.globalData.url;
Page({
// "navigationStyle":"custom"
  data: {
    title:"东港人社服务大厅",
    truefalse:true,
    mobile:'',
		code:'',
		FScode:'',
		// 倒计时参数
		timeStart:60, //倒计时初始值
    timeCur:null, //当前倒计时显示值
    ifTimeIn:false, //是否倒计时中
		success:false,
		blurts:false,
		clurts:false,
		loginType:'phone',
		pwdForm:{
			userName:'',
			password:'',
		},
		userNameErr:'',
		pwdErr:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({phone:options.phone})
  },
  dadafed(e){
    var that = this
    that.setData({
      truefalse:!that.data.truefalse
    })
  },
  onShow(){
		
	},
	changeLoginType(){
		this.setData({
			loginType: this.data.loginType === 'phone' ? 'pwd' : 'phone'
		})
	},
  // 获取手机号
  setMobile(e){
    let that = this
		// console.log(e.detail.value);
		that.setData({
			mobile : e.detail.value.replace(/\s+/g,"")//去空格
		});
  },
  setCode(e){
    let that = this
		that.setData({
      code : e.detail.value.replace(/\s+/g,""),
		});
  },
	setUserName(e){
		this.setData({
			'pwdForm.userName': e.detail.value
		})
	},
	setPwd(e){
		this.setData({
			'pwdForm.password': e.detail.value
		})
	},
	onUserNameBlur(e){
		if(e.detail.value){
			this.setData({
				userNameErr:''
			})
			return
		} 
		this.setData({
			userNameErr:'请输入用户名'
		})
	},
	onPwdBlur(e){
		if(e.detail.value){
			this.setData({
				pwdErr:''
			})
			return
		} 
		this.setData({
			pwdErr:'请输入用户密码'
		})
	},
  // 倒计时
	setTime(){
		let timeCur = this.data.timeCur - 1;
		// console.log(timeCur);
		if(timeCur < 0){
			clearInterval(this.data.timer);
			this.setData({
				ifTimeIn:false
			});
			return false;
		}
		this.setData({
			timeCur : timeCur
		});
  },
  // 获取验证码
	getMobileVerify(){
		var that = this
		if(!that.data.mobile){
			wx.showModal({
				title: '友情提示',
				content: '请输入手机号',
				showCancel: false,
			});
			return false
    }
		if(!/^1\d{10}$/.test(that.data.mobile)){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确的手机号',
				showCancel: false,
			});
			return false;
		}
		wx.showLoading({
		  title: "发送中",
		  mask: true
		});
		wx.request({
      url:url+'enterprise/sendSmsMessage',
      method: "GET",
      data:{
				mobile_no:that.data.mobile
			},
      success:function(res){
        var ret = res.data
        if(ret.code == 200){
					/* ----请求后台发送验证码成功---- */
					var str=ret.msg;//截取后4位
					var FScode = str.substring(str.length-6);
					console.log('短信验证码 ', FScode)
					// 执行倒计时
					that.setData({
						timeCur : that.data.timeStart,
						timer : setInterval(that.setTime,1000),
						ifTimeIn : true,
						success:true,
						FScode:FScode
					});
					/* ----请求后台发送验证码成功---- */
					wx.hideLoading();
					wx.showToast({
            title: '发送成功',
            icon:'none'
					})
        }else{
          wx.showToast({
            title: '发送失败',
            icon:'none'
					})
					wx.hideLoading();
        }
      }
    })
  },
  // 确定提交
	bindDo(){
		if(this.data.loginType === 'pwd'){
			this.pwdLogin()
			return
		}
		var that = this
		that.setData({ success:false})
		var phone = that.data.mobile
		var FScode = that.data.FScode
		// wx.navigateTo({
		// 	url: '/Company/newqiye/newqiye',
		// })
    if(!that.data.mobile){
			wx.showModal({
				title: '友情提示',
				content: '请输入手机号',
				showCancel: false,
			});
			return false
    }
		if(!/^1\d{10}$/.test(that.data.mobile)){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确的手机号',
				showCancel: false,
			});
			return false;
		}
    if(!that.data.code || FScode != that.data.code){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确验证码',
				showCancel: false,
			});
      that.setData({ success:true})
			return false;
		}
    var tf = that.data.truefalse
    if(!tf){
      wx.showToast({
        title: '请阅读并同意《服务条款及隐私政策》',
        icon: 'none',
        duration: 2000
      });
      wx.hideLoading()
      return false
    }
		wx.request({
			url:url+'enterprise/mobileNoLogin',//up_open_id_by_phone//user_login
			method: "POST",
			data:{
				smsCode:that.data.code,
				mobileNum:phone,
			},
			success(res){
				let resData = res.data
				if(resData.code === 422){
					wx.showModal({
						title: '友情提示',
						content: '当前手机号尚未注册企业，是否立即入驻新企业?',
						success (res) {
							if (res.confirm) {
								wx.navigateTo({
									url: '/Company/newqiye/newqiye',
								})
							}
						}
					});
					return
				}
				if(resData.code === 408 && !resData.data.enterpriseUid){
					wx.navigateTo({
						url: '/Company/newqiye/newqiye',
					})
					return
				}
				if(resData.code !== 200 && resData.code != 408){
					wx.showToast({
						title: resData.msg || '登录失败',
						icon: 'none',
						duration: 1500
					})
					return
				}
				// 状态 200成功，但无企业ID，说明是个人用户
				if(!resData.data.enterpriseUid){
					wx.showToast({
						title: '个人用户，无法登录企业端，请重新输入',
						icon: 'none',
						duration: 2000
					})
					return
				}

				wx.setStorageSync('id', res.data.data.id)
				wx.setStorageSync('userInfo', res.data.data)
				wx.setStorageSync('userType', EnumUserType.QiYe)
				
				wx.showToast({
					title: '登录成功',
					icon: 'none',
					duration: 1500
				});
				setTimeout(function(){
					wx.reLaunch({
						url: '/pages/index/index',
					})
				},1500)
					
			}
		})
	},
	pwdLogin(){
		if(!this.data.pwdForm.userName){
			wx.showToast({
        title: '用户名不能为空',
        icon: 'none',
        duration: 2000
      });
			return
		}
		if(!this.data.pwdForm.password){
			wx.showToast({
        title: '登录密码不能为空',
        icon: 'none',
        duration: 2000
      });
			return
		}

		var tf = this.data.truefalse
    if(!tf){
      wx.showToast({
        title: '请阅读并同意《服务条款及隐私政策》',
        icon: 'none',
        duration: 2000
      });
      return false
    }
		wx.showLoading({ title: '正在登录...'})
		wx.request({
			url:url+'enterprise/login',
			method: "POST",
			data:{
				account: this.data.pwdForm.userName,
				password: util.strEncode(this.data.pwdForm.password),
			},
			success(res){
				let resData = res.data
				if(resData.code === 422){
					wx.showModal({
						title: '友情提示',
						content: '当前手机号尚未注册企业，是否立即入驻新企业?',
						success (res) {
							if (res.confirm) {
								wx.navigateTo({
									url: '/Company/newqiye/newqiye',
								})
							}
						}
					});
					return
				}
				if(resData.code === 408 && !resData.data.enterpriseUid){
					wx.navigateTo({
						url: '/Company/newqiye/newqiye',
					})
					return
				}
				if(resData.code !== 200 && resData.code != 408){
					wx.showToast({
						title: resData.msg || '登录失败',
						icon: 'none',
						duration: 1500
					})
					return
				}
				// 状态 200成功，但无企业ID，说明是个人用户
				if(!resData.data.enterpriseUid){
					wx.showToast({
						title: '个人用户，无法登录企业端，请重新输入',
						icon: 'none',
						duration: 2000
					})
					return
				}

				wx.setStorageSync('id', res.data.data.id)
				wx.setStorageSync('userInfo', res.data.data)
				wx.setStorageSync('userType', EnumUserType.QiYe)
				
				wx.showToast({
					title: '登录成功',
					icon: 'none',
					duration: 1500
				});
				setTimeout(function(){
					wx.reLaunch({
						url: '/pages/index/index',
					})
				},1500)
					
			}
		})
	},
  // 手机号验证
	phoneOnBlur(e){
		var that = this
		var blur = e.detail.value
		if(blur == ''){
			that.setData({blurts:true})
		}else{
			that.setData({blurts:false})
		}
	},
	// 验证码验证
	codeOnBlur(e){
		var that = this
		var clur = e.detail.value
		if(clur == ''){
			console.log('1')
			that.setData({clurts:true})
		}else{
			that.setData({clurts:false})
		}
	},
  //条框协议
  des(){
    wx.navigateTo({
      url: '/HomePage/ab_tk/ab_tk?id='+1,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  // onReady: function () {

  // },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  // onHide: function () {

  // },

  /**
   * 生命周期函数--监听页面卸载
   */
  // onUnload: function () {

  // },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})